/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.jinyao.exploit.modules.gen.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.google.common.collect.Lists;
import com.jinyao.exploit.common.base.entity.TreeEntity;
import com.jinyao.exploit.common.utils.StringUtils;
import lombok.Data;

import java.util.List;

/**
 * 业务表Entity
 * @author ThinkGem
 * @version 2013-10-15
 */
@Data
@TableName(value = "gen_table")
public class Table extends TreeEntity<Table> {
	
	private String name; 	// 名称
	private String comments;		// 描述
	private String javaName;		// 实体类名称

	@TableField(exist = false)
	private List<TableColumn> columnList = Lists.newArrayList();	// 表列
	@TableField(exist = false)
	private String nameLike; 	// 按名称模糊查询
	@TableField(exist = false)
	private List<String> pkList; // 当前表主键列表
    @TableField(exist = false)
	private List<Table> childList = Lists.newArrayList();	// 子表列表
	
	public Table() {
		super();
	}

	public Table(long id){
		super(id);
	}
	
	/**
	 * 获取列名和说明
	 * @return
	 */
	public String getNameAndComments() {
		return getName() + (comments == null ? "" : "  :  " + comments);
	}

	/**
	 * 获取导入依赖包字符串
	 * @return
	 */
	public List<String> getImportList(){
		List<String> importList = Lists.newArrayList(); // 引用列表
		for (TableColumn column : getColumnList()){
			if (column.getIsNotBaseField() || ("1".equals(column.getIsQuery()) && "between".equals(column.getQueryType())
							&& ("createDate".equals(column.getSimpleJavaField()) || "updateDate".equals(column.getSimpleJavaField())))){
				// 导入类型依赖包， 如果类型中包含“.”，则需要导入引用。
				if (StringUtils.indexOf(column.getJavaType(), ".") != -1 && !importList.contains(column.getJavaType())){
					importList.add(column.getJavaType());
				}
			}
			if (column.getIsNotBaseField()){
				// 导入JSR303、Json等依赖包
				for (String ann : column.getAnnotationList()){
					if (!importList.contains(StringUtils.substringBeforeLast(ann, "("))){
						importList.add(StringUtils.substringBeforeLast(ann, "("));
					}
				}
			}
		}
		// 如果有子表，则需要导入List相关引用
		if (getChildList() != null && getChildList().size() > 0){
			if (!importList.contains("java.util.List")){
				importList.add("java.util.List");
			}
			if (!importList.contains("com.google.common.collect.Lists")){
				importList.add("com.google.common.collect.Lists");
			}
		}
		return importList;
	}
}


